<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 系统公共数据获取
 * 
 */
namespace osc\common\service;
use think\Db;
class System{	
	
	/**
     * object 对象实例
     */
    private static $instance;
	
	//禁外部实例化
	private function __construct(){}
	
	//单例模式	
	public static function getInstance(){    
        if (!(self::$instance instanceof self))  
        {  
            self::$instance = new self();  
        }  
        return self::$instance;  
    }
	//禁克隆
	private function __clone(){} 
    
	//取得系统配置分组列表
	public function get_config_module(){
	 	
		if (!$config_module = cache('config_module')) {
		
			$list=Db::name('config')->field('module,module_name')->group('module')->select();
			
			cache('config_module', $list);	
			
			$config_module=$list;
		}
		return $config_module;		
	}	
	//取得文章分类树形结构
	public function get_article_category_tree(){	
		$tree=new \oscshop\Tree();	
		return $tree->toFormatTree(Db::name('article_category')->field('id,pid,name')->select(),'name');
	}

	//取得文章分类
	public function get_article_category(){
			
		if(!$category= cache('article_category')){
			$category=list_to_tree(Db::name('article_category')->field('id,pid,name')->order('sort_order asc')->select());
			cache('article_category', $category);
		}	
			
		return $category;
	}
	//取得文章分类key值
	public function get_article_category_key($id,$key){
		if(!$category=cache('article_category_info')){
			$list=Db::name('article_category')->where('pid',0)->order('sort_order asc')->select();
			
			foreach ($list as $k => $v) {
				$category[$v['id']]=$v;
			}
			
			cache('article_category_info',$category);
		}
		if(isset($category[$id][$key])){
			return $category[$id][$key];
		}else{
			return null;
		}
	}

	
	
	//取得轮播图数据
	public function get_plugin_slide(){			
		if(!$slide=cache('plugin_slide')){
			$slide=Db::name('plugin_slide')->where(['status'=>1])->select();
			cache('plugin_slide', $slide);
		}				
		return $slide;
	}
	//搜索框下面的关键字
	public function get_search_key(){
		
		return explode(' ',config('search_key'));
		
	}
	//取得 快递/物流 公司代码
	function get_express($key=null){
		if (!$express = cache('express')) {		
			$list=Db::name('express')->select();		
			foreach ($list as $k => $v) {
				$express[$v['code']]=$v['name'];
			}		
			cache('express', $express);		
		}	
		if($key){		
			$ret=Db::name('express')->where('code',$key)->find();
			if($ret)
			{
				return $ret['name'];
			}
		}


	}
	function get_shipping($id,$key){
		if (!$shipping_list = cache('shipping_list')) {
			
			$list=Db::name('transport')->select();
			
			foreach ($list as $k => $v) {
				$shipping[$v['id']]=$v;
			}
			cache('shipping_list', $shipping);	
			$shipping_list=$shipping;
		}
		if(!isset($shipping_list[$id][$key])){
			return null;
		}
		return $shipping_list[$id][$key];	
	}
}
